package com.sjr.easy.chat.service.impl;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.sjr.easy.chat.constants.FriendRequestReadStatusConstants;
import com.sjr.easy.chat.model.Contact;
import com.sjr.easy.chat.model.FriendRequest;
import com.sjr.easy.chat.repo.FriendRequestRepo;
import com.sjr.easy.chat.service.ContactService;
import com.sjr.easy.chat.service.FriendRequestService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
@Transactional
public class FriendRequestServiceImpl implements FriendRequestService {

    @Autowired
    private FriendRequestRepo friendRequestRepo;

    @Autowired
    private ContactService contactService;

    @Override
    public void addFriendRequest(FriendRequest friendRequest) {
        log.info("friendRequest:{}", JSON.toJSONString(friendRequest));
        friendRequestRepo.save(friendRequest);
    }

    @Override
    public FriendRequest selectById(Long id) {
        return friendRequestRepo.getById(id);
    }

    @Override
    public List<FriendRequest> selectByFromUserIdOrTargetUserId(Long userId) {
        log.info("userId:{}", userId);
        LambdaQueryWrapper<FriendRequest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FriendRequest::getFromUserId, userId)
                .or()
                .eq(FriendRequest::getTargetUserId, userId);
        return friendRequestRepo.list(queryWrapper);
    }

    @Override
    public List<FriendRequest> selectByTargetUserId(Long userId) {
        log.info("targetUserId:{}", userId);
        LambdaQueryWrapper<FriendRequest> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(FriendRequest::getTargetUserId, userId);
        return friendRequestRepo.list(queryWrapper);
    }

    @Override
    public void batchChangeReadStatus2Read(List<Long> idList) {
        log.info("idList:{}", JSON.toJSONString(idList));
        if (CollectionUtils.isEmpty(idList)) {
            return;
        }
        ArrayList<FriendRequest> paramsList = new ArrayList<>();
        for (Long id : idList) {
            FriendRequest friendRequest = new FriendRequest();
            friendRequest.setId(id);
            friendRequest.setReadStatus(FriendRequestReadStatusConstants.read);
            paramsList.add(friendRequest);
        }
        friendRequestRepo.updateBatchById(paramsList, 100);
    }

    @Override
    public void acceptFriendRequest(Long friendRequestId) {

        FriendRequest friendRequest = selectById(friendRequestId);

        FriendRequest params = new FriendRequest();
        params.setId(friendRequestId);
        params.setStatus(1);
        friendRequestRepo.updateById(params);

        Contact fromContact = new Contact();
        fromContact.setUserId(friendRequest.getFromUserId());
        fromContact.setContactUserId(friendRequest.getTargetUserId());
        fromContact.setCreateTime(new Date());
        contactService.addContact(fromContact);

        Contact toContact = new Contact();
        toContact.setUserId(friendRequest.getTargetUserId());
        toContact.setContactUserId(friendRequest.getFromUserId());
        toContact.setCreateTime(new Date());
        contactService.addContact(toContact);
    }

    @Override
    public void rejectFriendRequest(Long friendRequestId) {
        FriendRequest params = new FriendRequest();
        params.setId(friendRequestId);
        params.setStatus(2);
        friendRequestRepo.updateById(params);
    }
}
